package com.wbcs.system.permission;

import com.wbcs.config.component.IComponentConfigBean;
import com.wbcs.util.Consts;

public class ComponentPartPermissionBean extends AbsPermissionBean
{
    protected String id;
    
    protected AbsPermissionBean parentPermissionBean;

    public ComponentPartPermissionBean(String id,AbsPermissionBean parentPermissionBean)
    {
        this.id=id;
        this.parentPermissionBean=parentPermissionBean;
    }

    public String getId()
    {
        return id;
    }

    public AbsPermissionBean getParentPermissionBean()
    {
        return parentPermissionBean;
    }

    @Override
	protected IComponentConfigBean getComponentConfigBean()
    {
        if(parentPermissionBean instanceof ComponentPermissionBean)
        {
            return ((ComponentPermissionBean)parentPermissionBean).getComponentConfigBean();
        }
        return ((ComponentPartPermissionBean)parentPermissionBean).getComponentConfigBean();
    }
    
    public void authorize(String partid,String permissiontype,String permissionvalue)
    {
        if(partid==null||partid.trim().equals(""))
        {
            addPermission(permissiontype,permissionvalue);
        }else
        {
            this.getChildPermissionBean(partid,true).authorize(null,permissiontype,permissionvalue);
        }
    }
    
    int checkPermission(String partid,String permissiontype,String permissionvalue)
    {
        if(partid==null||partid.trim().equals(""))
        {
            String myPermissionvalue=this.getPermission(permissiontype);
            if(myPermissionvalue==null||myPermissionvalue.trim().equals("")) return Consts.CHKPERMISSION_EMPTY;
            if(myPermissionvalue.equalsIgnoreCase(permissionvalue))
            {
                return Consts.CHKPERMISSION_YES;
            }else
            {
                return Consts.CHKPERMISSION_NO;
            }
        }else
        {
            ComponentPartPermissionBean cpauthBean=this.getChildPermissionBean(partid,false);
            if(cpauthBean==null)
            {
                return Consts.CHKPERMISSION_EMPTY;
            }else
            {
                return cpauthBean.checkPermission(null,permissiontype,permissionvalue);
            }
        }
    }
}
